package com.project.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.entity.BorrowReturnInfo;
import com.project.entity.DeptInfo;
import com.project.entity.ReaderInfo;
import com.project.entity.Role;
import com.project.service.BorrowReturnInfoService;
import com.project.service.DeptInfoService;
import com.project.service.ReaderInfoService;
import com.project.service.RoleService;

/**
 * @author jackiel
 * 
 */
@Controller
@RequestMapping(value = "/readerinfo")
public class ReaderInfoControler {

	@Resource(name = "readerInfoService")
	private ReaderInfoService service;

	@Resource(name = "deptInfoService")
	private DeptInfoService deptService;

	@Resource(name = "roleService")
	private RoleService roleService;

	@Resource(name = "borrowReturnInfoService")
	private BorrowReturnInfoService brService;

	@RequestMapping(value = "/delete")
	public String delete(HttpSession session, String id) {
		ReaderInfo readerInfo1 = service.get(Integer.parseInt(id));
		ReaderInfo readerInfo = (ReaderInfo) session.getAttribute("readerInfo");

		readerInfo1.setDeleteReader(readerInfo);
		readerInfo1.setDeleteDateTime(new Date());

		service.update(readerInfo1);

		return "redirect:/readerinfo/list";
	}

	/**
	 * @param model
	 * @param reader
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String add(Model model, ReaderInfo reader) {
		Integer id = reader.getId();
		List<Role> rlist = roleService.listAll();
		model.addAttribute("roleList", rlist);
		List<DeptInfo> list = deptService.listAll();
		model.addAttribute("deptList", list);
		if (id != null) {
			reader = service.get(id);
			model.addAttribute("readerInfo", reader);
			return "readerinfo/edit";
		}
		return "readerinfo/add";
	}

	@RequestMapping(value = "/myEdit")
	public String edit(HttpSession session, Model model) {
		List<Role> rlist = roleService.listAll();
		model.addAttribute("roleList", rlist);
		List<DeptInfo> list = deptService.listAll();
		model.addAttribute("deptList", list);
		ReaderInfo reader = (ReaderInfo) session.getAttribute("readerInfo");
		String name = reader.getName();
		List<BorrowReturnInfo> brList = brService.get(name);
		model.addAttribute("brList", brList);
		return "readerinfo/edit";
	}

	/**
	 * @param readerInfo
	 * @param deptName
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/save")
	public String save(ReaderInfo readerInfo, String deptName, String roleName,
			HttpSession session) {
		ReaderInfo readerInfo1 = (ReaderInfo) session
				.getAttribute("readerInfo");

		if (readerInfo.getId() == null) {
			Date date = new Date();
			DeptInfo deptInfo = deptService.get(Integer.parseInt(deptName));
			readerInfo.setDeptId(deptInfo);
			Role role = roleService.get(Integer.parseInt(roleName));
			readerInfo.setRoleId(role);
			readerInfo.setPassword("000000");
			readerInfo.setEntryReader(readerInfo1);
			readerInfo.setEntryDateTime(date);
			service.saveOrUpdate(readerInfo);
		} else {

			DeptInfo deptInfo = deptService.get(Integer.parseInt(deptName));
			readerInfo.setDeptId(deptInfo);
			Role role = roleService.get(Integer.parseInt(roleName));
			readerInfo.setRoleId(role);

			readerInfo1.setName(readerInfo.getName());
			readerInfo1.setSex(readerInfo.getSex());
			readerInfo1.setPhone(readerInfo.getPhone());
			readerInfo1.setEmail(readerInfo.getEmail());
			readerInfo1.setDeptId(readerInfo.getDeptId());
			readerInfo1.setRoleId(readerInfo.getRoleId());

			service.saveOrUpdate(readerInfo1);
		}
		return "redirect:/readerinfo/list";
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String List(Model model) {

		List<ReaderInfo> list = service.getReaderList();
		model.addAttribute("readerlist", list);
		return "readerinfo/list";
	}

	@RequestMapping(value = "/passwordChangeGoto")
	public String passwordChangeGoto() {
		return "readerinfo/passwordchange";
	}

	@RequestMapping(value = "/passwordChange")
	@ResponseBody
	public Map<String, String> passwordChange(
			@RequestBody Map<String, String> mapParams,
			HttpServletRequest request) {
		ReaderInfo readerInfo = (ReaderInfo) request.getSession().getAttribute(
				"readerInfo");
		Map<String, String> map = new HashMap<String, String>();
		if (!readerInfo.getPassword().equals(mapParams.get("password"))) {
			map.put("oldEmpty", "Please input your old password");
		} else if (!mapParams.get("password1").equals(
				mapParams.get("password2"))) {
			map.put("differ", "Entered passwords differ");
		} else {
			readerInfo.setPassword(mapParams.get("password1").toString());
			service.saveOrUpdate(readerInfo);
		}
		return map;
	}
}
